<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/10
 * Time: 15:40
 */

namespace app\admin\controller;


use think\Controller;

class Cp extends Controller
{
    /**
     * 重庆时时彩
     * http://cp.pf09.top/admin/Cp/ssc
     */
    public function ssc(){
        echo "I'm Comeing \n";
        //跳出定时判定
        $info= db('config')->where('id',1)->find();
        $sscOpen = $info['code_open'];
        if($sscOpen == 1){
            // 提前一天插入120条第二天要开奖的数据
            $expect = date('Ymd',time()+3600*24) . '%';
            if(db('ssc')->where('expect','like',$expect)->count() == 0){
                for ($i = 119; $i>=0 ;$i--){
                    if($i<10){
                        $i = '0'.$i;
                    }
                    if ($i >= 100){
                        $data['expect'] = date('Ymd',time()+3600*24).$i;
                    }else{
                        $data['expect'] = date('Ymd',time()+3600*24) . '0'.$i;
                    }
                    $data['expect'] ++;
                    $data['opencode'] = '--';
                    $data['opentime'] = '--';
                    $data['opentimestamp'] = '--';
                    $data['handcode'] = '--';
                    $data['state'] = 2;
                    db('ssc')->insert($data);
                }
            }
            //采集接口彩票今天数据 对比入库
            $date = date("Y-m-d");
            //处理开奖延迟问题
            $today = strtotime(date("Y-m-d"),time());//0点时间戳
            $time = time() - $today;
            if($time < 300){
                $date = date("Y-m-d",strtotime("-1 day"));
            }
            $url = "http://ho.apiplus.net/daily.do?token=t2a3a08213f3df40ck&code=cqssc&format=json&date=$date";
            $data1 = @file_get_contents($url);
            if(!trim($data1)){
                echo date('Y-m-d h:i:s').'未获取到接口数据';
                error_log(date('Y-m-d h:i:s').'未获取到接口数据',0);
                return;
            }
            $data1 = json_decode($data1,true);
            $data1 = $data1['data'];
            foreach ($data1 as $v){
                $find = db('ssc')->where('expect',$v['expect'])->find();
                //如果有这条数据
                if($find){
                    //其数据开奖结果和接口不同就更新入库
                    if($find['opencode'] != $v['opencode']){
                        $v['state'] = 1;
                        $qishu = $v['expect'];
                        db('ssc')->where('expect',$v['expect'])->update($v);
                        db('bet')->where(['expect'=>$qishu,'code_type'=>2])->update(['state'=>1]);
                        //用户中奖返钱
                        //赔率
                        $odds = db('webconfig')->where('id',1)->value('odds');
                        //彩票价钱
                        $codemoney =  db('webconfig')->where('id',1)->value('code_money');
                        if(db('ssc')->where('expect',$qishu)->value('handcode') != '--'){
                            $opencode = db('ssc')->where('expect',$qishu)->value('handcode');
                        }else{
                            $opencode = db('ssc')->where('expect',$qishu)->value('opencode');
                        }
                        //个位中奖
                        $kge =  substr($opencode,-1);
                        $kgeuser = db('bet')->where(['expect'=>$qishu,'seat'=>1,'code_type'=>2])->where('content','like',"%$kge%")->select();
                        if(!$kgeuser){
                            $kgeuser = [];
                        }
                        foreach ($kgeuser as $k=>$v){
                            $id = $v['id'];
                            $uid = $v['uid'];
                            $number = $v['number'];
                            //用户中奖金额
                            $zmoney = $odds * $codemoney * $number;
                            db('bet')->where('id',$id)->update(['winning'=>$zmoney]);
                            db('user')->where('id',$uid)->setInc('money',$zmoney);
                        }
                        //十位中奖
                        $kshi =  substr($opencode,-3,1);
                        $kshiuser = db('bet')->where(['expect'=>$qishu,'seat'=>2,'code_type'=>2])->where('content','like',"%$kshi%")->select();
                        if(!$kshiuser){
                            $kshiuser = [];
                        }
                        foreach ($kshiuser as $k=>$v){
                            $id = $v['id'];
                            $uid = $v['uid'];
                            $number = $v['number'];
                            //用户中奖金额
                            $zmoney = $odds * $codemoney * $number;
                            db('bet')->where('id',$id)->update(['winning'=>$zmoney]);
                            db('user')->where('id',$uid)->setInc('money',$zmoney);
                        }
                        //百位中奖
                        $kbai =  substr($opencode,-5,1);
                        $kbaiuser = db('bet')->where(['expect'=>$qishu,'seat'=>3,'code_type'=>2])->where('content','like',"%$kbai%")->select();
                        if(!$kbaiuser){
                            $kbaiuser = [];
                        }
                        foreach ($kbaiuser as $k=>$v){
                            $id = $v['id'];
                            $uid = $v['uid'];
                            $number = $v['number'];
                            //用户中奖金额
                            $zmoney = $odds * $codemoney * $number;
                            db('bet')->where('id',$id)->update(['winning'=>$zmoney]);
                            db('user')->where('id',$uid)->setInc('money',$zmoney);
                        }
                        //千位中奖
                        $kqian =  substr($opencode,-7,1);
                        $kqianuser = db('bet')->where(['expect'=>$qishu,'seat'=>4,'code_type'=>2])->where('content','like',"%$kqian%")->select();
                        if(!$kqianuser){
                            $kqianuser = [];
                        }
                        foreach ($kqianuser as $k=>$v){
                            $id = $v['id'];
                            $uid = $v['uid'];
                            $number = $v['number'];
                            //用户中奖金额
                            $zmoney = $odds * $codemoney * $number;
                            db('bet')->where('id',$id)->update(['winning'=>$zmoney]);
                            db('user')->where('id',$uid)->setInc('money',$zmoney);
                        }
                        //万位中奖
                        $kwan =  substr($opencode,-9,1);
                        $kwannuser = db('bet')->where(['expect'=>$qishu,'seat'=>5,'code_type'=>2])->where('content','like',"%$kwan%")->select();
                        if(!$kwannuser){
                            $kwannuser = [];
                        }
                        foreach ($kwannuser as $k=>$v){
                            $id = $v['id'];
                            $uid = $v['uid'];
                            $number = $v['number'];
                            //用户中奖金额
                            $zmoney = $odds * $codemoney * $number;
                            db('bet')->where('id',$id)->update(['winning'=>$zmoney]);
                            db('user')->where('id',$uid)->setInc('money',$zmoney);
                        }

                        //向彩票数据库插入中奖金额
                        $summoney =  db('bet')->where(['expect'=>$qishu,'code_type'=>2])->sum('winning');
                        db('ssc')->where('expect',$qishu)->update(['winningmoney'=>$summoney]);
                        //向彩票数据库插入参与人数
                        $sumuser = db('bet')->where(['expect'=>$qishu,'code_type'=>2])->group('uid')->count();
                        db('ssc')->where('expect',$qishu)->update(['sumuser'=>$sumuser]);



                    }
                }else{//如果没有今天提前插入的数据就插入
                    $expect = date('Ymd') . '%';
                    if(db('ssc')->where('expect','like',$expect)->count() == 0){
                        for ($i = 119; $i>=0 ;$i--){
                            if($i<10){
                                $i = '0'.$i;
                            }
                            if ($i >= 100){
                                $data2['expect'] = date('Ymd').$i;
                            }else{
                                $data2['expect'] = date('Ymd') . '0'.$i;
                            }
                            $data2['expect'] ++;
                            $data2['opencode'] = '--';
                            $data2['opentime'] = '--';
                            $data2['opentimestamp'] = '--';
                            $data2['handcode'] = '--';
                            $data2['state'] = 2;
                            db('ssc')->insert($data2);
                        }
                    }
                }
            }
        }
    }
    
    public function ptc(){
        //跳出定时判定
        $info= db('config')->where('id',2)->find();
        $sscOpen = $info['code_open'];
        if($sscOpen == 1){
            // 提前一天插入288条第二天要开奖的数据
            $expect = date('Ymd',time()+3600*24) . '%';
            if(db('ptc')->where('expect','like',$expect)->count() == 0){
                for ($i = 287; $i>=0 ;$i--){
                    if($i<10){
                        $i = '0'.$i;
                    }
                    if ($i >= 100){
                        $data['expect'] = date('Ymd',time()+3600*24).$i;
                    }else{
                        $data['expect'] = date('Ymd',time()+3600*24) . '0'.$i;
                    }
                    $data['expect'] ++;
                    $data['opencode'] = '--';
                    $data['opentime'] = '--';
                    $data['opentimestamp'] = '--';
                    $data['handcode'] = '--';
                    $data['state'] = 2;
                    db('ptc')->insert($data);
                }
            }
            //插入今天数据
            $expect2 = date('Ymd') . '%';
            if(db('ptc')->where('expect','like',$expect2)->count() == 0){
                for ($i = 287; $i>=0 ;$i--){
                    if($i<10){
                        $i = '0'.$i;
                    }
                    if ($i >= 100){
                        $data2['expect'] = date('Ymd').$i;
                    }else{
                        $data2['expect'] = date('Ymd') . '0'.$i;
                    }
                    $data2['expect'] ++;
                    $data2['opencode'] = '--';
                    $data2['opentime'] = '--';
                    $data2['opentimestamp'] = '--';
                    $data2['handcode'] = '--';
                    $data2['state'] = 2;
                    db('ptc')->insert($data2);
                }
            }
            //计算当前期数
            $s =date('H')*60+date('i');
            $s =intval($s/5);
            $s = substr(strval($s+1000),1,3); ;
            $qishu = date('Ymd').$s;
            if($s == 0){
                $qishu = date('Ymd').'288';
            }


            //如果已经开过奖
            if(db('ptc')->where('expect',$qishu)->value('opencode') != '--'){
                return;
            }
            //如果还未到开奖时间
            if(date('i') % 5 != 4 || date('s') < 30){
                return;
            }


            //计算开奖数据
            $code_style = db('webconfig')->where('id',1)->value('code_style');
            //随机模式
            if($code_style == 3){
                $code_data = mt_rand(10000,99999);
                $codearr = str_split($code_data);
                $code_data = implode(',',$codearr);
                $code_ptc['opencode'] =$code_data;
                $code_ptc['opentime'] = date('Y-m-d h:i:s;');
                $code_ptc['opentimestamp'] = time();
                $code_ptc['state'] = 1;
                db('ptc')->where('expect',$qishu)->update($code_ptc);
                db('bet')->where(['expect'=>$qishu,'code_type'=>1])->update(['state'=>1]);
            }elseif ($code_style == 1){//杀大赔小模式
                //个位
                $gearr = db('bet')->where('expect',$qishu)->where('seat',1)->column('content');
                if($gearr){
                    $ge = mt_rand(0,9);
                    $g = implode($gearr);
                    $ci = 100000;
                    for ($i = 0;$i<10;$i++){
                        if($ci > substr_count($g,$i)){
                            $ge = $i;
                            $ci = substr_count($g,$i);
                        }
                    }
                }else{
                    $ge = mt_rand(0,9);
                }
                //十位
                $shiarr = db('bet')->where('expect',$qishu)->where('seat',2)->column('content');
                if($shiarr){
                    $shi = mt_rand(0,9);
                    $g = implode($shiarr);
                    $ci = 100000;
                    for ($i = 0;$i<10;$i++){
                        if($ci > substr_count($g,$i)){
                            $shi = $i;
                            $ci = substr_count($g,$i);
                        }
                    }
                }else{
                    $shi = mt_rand(0,9);
                }
                //百位
                $baiarr = db('bet')->where('expect',$qishu)->where('seat',3)->column('content');
                if($baiarr){
                    $bai= mt_rand(0,9);
                    $g = implode($baiarr);
                    $ci = 100000;
                    for ($i = 0;$i<10;$i++){
                        if($ci > substr_count($g,$i)){
                            $bai = $i;
                            $ci = substr_count($g,$i);
                        }
                    }
                }else{
                    $bai = mt_rand(0,9);
                }
                //千位
                $qianarr = db('bet')->where('expect',$qishu)->where('seat',4)->column('content');
                if($qianarr){
                    $qian= mt_rand(0,9);
                    $g = implode($qianarr);
                    $ci = 100000;
                    for ($i = 0;$i<10;$i++){
                        if($ci > substr_count($g,$i)){
                            $qian = $i;
                            $ci = substr_count($g,$i);
                        }
                    }
                }else{
                    $qian = mt_rand(0,9);
                }
                //万位
                $wanarr = db('bet')->where('expect',$qishu)->where('seat',5)->column('content');
                if($wanarr){
                    $wan= mt_rand(0,9);
                    $g = implode($wanarr);
                    $ci = 100000;
                    for ($i = 0;$i<10;$i++){
                        if($ci > substr_count($g,$i)){
                            $wan = $i;
                            $ci = substr_count($g,$i);
                        }
                    }
                }else{
                    $wan = mt_rand(0,9);
                }
                $cod['opencode'] =$wan . ',' . $qian . ',' . $bai . ',' . $shi . ',' . $ge;
                $cod['opentime'] = date('Y-m-d h:i:s;');
                $cod['opentimestamp'] = time();
                $cod['state'] = 1;
                db('ptc')->where('expect',$qishu)->update($cod);
                db('bet')->where(['expect'=>$qishu,'code_type'=>1])->update(['state'=>1]);
            };
            //用户中奖返钱
            //赔率
            $odds = db('webconfig')->where('id',1)->value('odds');
            //彩票价钱
            $codemoney =  db('webconfig')->where('id',1)->value('code_money');
            if(db('ptc')->where('expect',$qishu)->value('handcode') != '--'){
                $opencode = db('ptc')->where('expect',$qishu)->value('handcode');
            }else{
                $opencode = db('ptc')->where('expect',$qishu)->value('opencode');
            }
            //个位中奖
            $kge =  substr($opencode,-1);
            $kgeuser = db('bet')->where(['expect'=>$qishu,'seat'=>1,'code_type'=>1])->where('content','like',"%$kge%")->select();
            if(!$kgeuser){
                $kgeuser = [];
            }
            foreach ($kgeuser as $k=>$v){
                $id = $v['id'];
                $uid = $v['uid'];
                $number = $v['number'];
                //用户中奖金额
                $zmoney = $odds * $codemoney * $number;
                db('bet')->where('id',$id)->update(['winning'=>$zmoney]);
                db('user')->where('id',$uid)->setInc('money',$zmoney);
            }
            //十位中奖
            $kshi =  substr($opencode,-3,1);
            $kshiuser = db('bet')->where(['expect'=>$qishu,'seat'=>2,'code_type'=>1])->where('content','like',"%$kshi%")->select();
            if(!$kshiuser){
                $kshiuser = [];
            }
            foreach ($kshiuser as $k=>$v){
                $id = $v['id'];
                $uid = $v['uid'];
                $number = $v['number'];
                //用户中奖金额
                $zmoney = $odds * $codemoney * $number;
                db('bet')->where('id',$id)->update(['winning'=>$zmoney]);
                db('user')->where('id',$uid)->setInc('money',$zmoney);
            }
            //百位中奖
            $kbai =  substr($opencode,-5,1);
            $kbaiuser = db('bet')->where(['expect'=>$qishu,'seat'=>3,'code_type'=>1])->where('content','like',"%$kbai%")->select();
            if(!$kbaiuser){
                $kbaiuser = [];
            }
            foreach ($kbaiuser as $k=>$v){
                $id = $v['id'];
                $uid = $v['uid'];
                $number = $v['number'];
                //用户中奖金额
                $zmoney = $odds * $codemoney * $number;
                db('bet')->where('id',$id)->update(['winning'=>$zmoney]);
                db('user')->where('id',$uid)->setInc('money',$zmoney);
            }
            //千位中奖
            $kqian =  substr($opencode,-7,1);
            $kqianuser = db('bet')->where(['expect'=>$qishu,'seat'=>4,'code_type'=>1])->where('content','like',"%$kqian%")->select();
            if(!$kqianuser){
                $kqianuser = [];
            }
            foreach ($kqianuser as $k=>$v){
                $id = $v['id'];
                $uid = $v['uid'];
                $number = $v['number'];
                //用户中奖金额
                $zmoney = $odds * $codemoney * $number;
                db('bet')->where('id',$id)->update(['winning'=>$zmoney]);
                db('user')->where('id',$uid)->setInc('money',$zmoney);
            }
            //万位中奖
            $kwan =  substr($opencode,-9,1);
            $kwannuser = db('bet')->where(['expect'=>$qishu,'seat'=>5,'code_type'=>1])->where('content','like',"%$kwan%")->select();
            if(!$kwannuser){
                $kwannuser = [];
            }
            foreach ($kwannuser as $k=>$v){
                $id = $v['id'];
                $uid = $v['uid'];
                $number = $v['number'];
                //用户中奖金额
                $zmoney = $odds * $codemoney * $number;
                db('bet')->where('id',$id)->update(['winning'=>$zmoney]);
                db('user')->where('id',$uid)->setInc('money',$zmoney);
            }
            //向彩票数据库插入中奖金额
            $summoney =  db('bet')->where(['expect'=>$qishu,'code_type'=>1])->sum('winning');
            db('ptc')->where('expect',$qishu)->update(['winningmoney'=>$summoney]);
            //向彩票数据库插入参与人数
            $sumuser = db('bet')->where(['expect'=>$qishu,'code_type'=>1])->group('uid')->count();
            db('ptc')->where('expect',$qishu)->update(['sumuser'=>$sumuser]);
        }
    }





}